package com.linzhaowei.headhuntingservice;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.chat.EMClient;
import com.linzhaowei.headhuntingservice.bean.Applicant;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;

/**
 * 求职者更改个人信息
 */
public class ApplicantUpdatePersonal extends AppCompatActivity implements View.OnClickListener {

    private TextView title;
    private EditText name;
    private RadioGroup gender;
    private RadioButton choseGender;
    private EditText age;
    private EditText education;
    private EditText school;
    private EditText major;
    private EditText tel;
    private EditText resume;
    private Button btn;

    private Applicant applicant;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_improve_applicant);
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        initViews();
        setEvents();

        title.setText("修改个人信息");


        selectApplicant();

    }

    private void initViews() {
        title = findViewById(R.id.i_a_title);
        name = findViewById(R.id.i_a_name);
        gender = findViewById(R.id.i_a_gender);
        age = findViewById(R.id.i_a_age);
        education = findViewById(R.id.i_a_education);
        school = findViewById(R.id.i_a_school);
        major = findViewById(R.id.i_a_major);
        tel = findViewById(R.id.i_a_tel);
        resume = findViewById(R.id.i_a_resume);
        btn = findViewById(R.id.i_a_btn);
    }

    private void setEvents() {
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.i_a_btn:
                improveApplicant();

                break;
            default:
                break;

        }
    }

    private void improveApplicant() {
        applicant = new Applicant();
        applicant.setId(sharedPreferences.getInt("id", -1));
        applicant.setName(name.getText().toString().trim());
        choseGender = findViewById(gender.getCheckedRadioButtonId());
        applicant.setGender(choseGender.getText().toString());
        applicant.setAge(Integer.valueOf(age.getText().toString()));
        applicant.setEducation(education.getText().toString().trim());
        applicant.setSchool(school.getText().toString().trim());
        applicant.setMajor(major.getText().toString().trim());
        applicant.setTel(tel.getText().toString().trim());
        applicant.setResume(resume.getText().toString());
        String string = JSON.toJSONString(applicant);

        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();

                try {
                    String result = httpUtils.post(Ip.ip + "/user/improve_applicant", string);
                    if (result.equals("true")) {
                        if(!applicant.getName().isEmpty()) {
                            EMClient.getInstance().pushManager().updatePushNickname(applicant.getName());
                        }
                        runOnUiThread(() -> Toast.makeText(ApplicantUpdatePersonal.this, "修改成功", Toast.LENGTH_SHORT).show());
                        finish();

                    } else {
                        runOnUiThread(() -> Toast.makeText(ApplicantUpdatePersonal.this, "信息有误", Toast.LENGTH_SHORT).show());


                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    private void selectApplicant() {

        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();

                try {
                    String result = httpUtils.get(Ip.ip + "/user/select_applicant?id=" + String.valueOf(sharedPreferences.getInt("id", -1)));
                    JSONObject jsonObject = JSON.parseObject(result);
                    applicant = jsonObject.toJavaObject(Applicant.class);
                    show();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void show() {
        runOnUiThread(() -> {
            name.setText(applicant.getName());
            if (applicant.getGender().equals("女")) {
                choseGender = findViewById(R.id.i_a_female);
                choseGender.setChecked(true);
            }
            age.setText(applicant.getAge() + "");
            education.setText(applicant.getEducation());
            school.setText(applicant.getSchool());
            major.setText(applicant.getMajor());
            tel.setText(applicant.getTel());
            resume.setText(applicant.getResume());
        });
    }
}
